package helloWorld;

import model.uml.note.Note;
import model.util.Dimension;
import model.util.Point;

import org.mt4j.MTApplication;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.interfaces.IMTComponent3D;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.shapes.MTEllipse;
import org.mt4j.components.visibleComponents.shapes.MTLine;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.shapes.MTRectangle.PositionAnchor;
import org.mt4j.components.visibleComponents.shapes.MTRoundRectangle;
import org.mt4j.components.visibleComponents.widgets.MTClipRectangle;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.components.visibleComponents.widgets.keyboard.MTTextKeyboard;
import org.mt4j.input.gestureAction.DefaultDragAction;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.ToolsGeometry;
import org.mt4j.util.math.Vector3D;

import app.ViewConstants;

import view.mt4j.component.note.NoteViewImpl;

public class HelloWorldScene extends AbstractScene {
	MTTextArea textField;
	MTClipRectangle sheet;
	MTRoundRectangle innerRightRectangleA;
	MTRoundRectangle innerRightRectangleB;
	MTRoundRectangle leftRectangle;
	MTRoundRectangle rightRectangle;

	public HelloWorldScene(MTApplication mtApplication, String name) {
		super(mtApplication, name);

		
		// Disable frustum culling for this scene - optional
		// this.getCanvas().setFrustumCulling(false);
		// Set the background color
		this.setClearColor(ViewConstants.MENU_BACKGROUND_COLOR);

		drawGrid(mtApplication, mtApplication.height, mtApplication.width, 15,
				MTColor.GREY);

		float sheetX = 100f;
		float sheetY = 50f;
		sheet = new MTClipRectangle(mtApplication, sheetX,sheetY, 0, 800, 200);
		this.getCanvas().addChild(sheet);
		sheet.setFillColor(MTColor.WHITE);
		sheet.setStrokeColor(MTColor.BLACK);
		sheet.setStrokeWeight(1);
		//sheet.setAnchor(PositionAnchor.UPPER_LEFT);// 1
		//sheet.setPositionGlobal(new Vector3D(sheetX, sheetY));// 2

		float leftRectangleWidth = 200f;
		float leftRectangleHeight = 150f;
		leftRectangle = new MTRoundRectangle(mtApplication, 0, 0, 0,
				leftRectangleWidth, leftRectangleHeight, 7, 7);
		sheet.addChild(leftRectangle);
		leftRectangle.setFillColor(MTColor.WHITE);
		leftRectangle.setStrokeColor(MTColor.BLACK);
		leftRectangle.setPositionRelativeToParent(new Vector3D(
				leftRectangleWidth / 2f + 10f, leftRectangleHeight / 2f + 10f));

		float rightRectangleWidth = 500f;
		float rightRectangleHeight = 200f;
		rightRectangle = new MTRoundRectangle(mtApplication, 0, 0, 0,
				rightRectangleWidth, rightRectangleHeight, 7, 7);
		sheet.addChild(rightRectangle);
		rightRectangle.setFillColor(MTColor.WHITE);
		rightRectangle.setStrokeColor(MTColor.BLACK);
		rightRectangle.setPositionRelativeToParent(new Vector3D(
				rightRectangleWidth / 2f + 230f,
				rightRectangleHeight / 2f + 10f));

		float innerRightRectangleAWidth = 180f;
		float innerRightRectangleAHeight = 150f;
		innerRightRectangleA = new MTRoundRectangle(mtApplication, 0, 0, 0,
				innerRightRectangleAWidth, innerRightRectangleAHeight, 7, 7);
		rightRectangle.addChild(innerRightRectangleA);
		innerRightRectangleA.setFillColor(MTColor.WHITE);
		innerRightRectangleA.setStrokeColor(MTColor.BLACK);
		innerRightRectangleA.setPositionRelativeToParent(new Vector3D(
				innerRightRectangleAWidth / 2f + 10f,
				innerRightRectangleAHeight / 2f + 10f));

		float innerRightRectangleBWidth = 200f;
		float innerRightRectangleBHeight = 100f;
		innerRightRectangleB = new MTRoundRectangle(mtApplication, 0, 0, 0,
				innerRightRectangleBWidth, innerRightRectangleBHeight, 7, 7);
		rightRectangle.addChild(innerRightRectangleB);
		innerRightRectangleB.setFillColor(MTColor.WHITE);
		innerRightRectangleB.setStrokeColor(MTColor.BLACK);
		innerRightRectangleB.setPositionRelativeToParent(new Vector3D(
				innerRightRectangleBWidth / 2f + 10f
						+ innerRightRectangleAWidth + 70f,
				innerRightRectangleBHeight / 2f + 10f));

		MTTextKeyboard keyboard = new MTTextKeyboard(mtApplication);
		keyboard.setPositionGlobal(new Vector3D(mtApplication.width / 2f, 600));
		this.getCanvas().addChild(keyboard);

		// Create a textfield
		textField = new MTTextArea(mtApplication, FontManager.getInstance()
				.createFont(mtApplication, "arial.ttf", 10, MTColor.GRAY));
		textField.setNoFill(true);
		textField.setNoStroke(true);
		textField.setText("ETAT A");
		textField.unregisterAllInputProcessors();
		textField.registerInputProcessor(new DragProcessor(mtApplication));
		textField.removeAllGestureEventListeners();// NEEDED FOR CLIPPING

		textField.addGestureListener(DragProcessor.class,
				new IGestureEventListener() {
					public boolean processGestureEvent(MTGestureEvent ge) {
						DragEvent de = (DragEvent) ge;

						textField.translateGlobal(de.getTranslationVect());

						Vector3D[] v1 = innerRightRectangleA
								.getVerticesGlobal();
						Vector3D[] v2 = textField.getBounds()
								.getVectorsGlobal();
						boolean inside = true;
						for (Vector3D vector3d : v2) {
							if (!ToolsGeometry.isPolygonContainsPoint(v1,
									vector3d)) {
								inside = false;
							}
						}

						if (!inside) {
							System.out.println("outside");
							textField.translateGlobal(de.getTranslationVect()
									.getScaled(-1));
						} else
							System.out.println("inside");
						return false;
					}
					// @Override
					// public boolean processGestureEvent(MTGestureEvent event)
					// {
					// DragEvent de = (DragEvent) event;
					// System.out.println(de.getTo());
					// switch (de.getId()) {
					// case MTGestureEvent.GESTURE_STARTED:
					// System.out.println("Drag gesture on component "
					// + de.getTargetComponent() + " started");
					// break;
					// case MTGestureEvent.GESTURE_UPDATED:
					// System.out.println("Drag gesture on component "
					// + de.getTargetComponent() + " updated");
					// break;
					// case MTGestureEvent.GESTURE_ENDED:
					// System.out.println("Drag gesture on component "
					// + de.getTargetComponent() + " ended");
					// break;
					// default:
					// break;
					// }
					// return false;
					// }
				});
		int len = 70;
		int arrowsize = 10;
		MTLine a = new MTLine(mtApplication, 180, 80, 180 + len, 80);
		a.setStrokeColor(MTColor.BLACK);
		innerRightRectangleA.addChild(a);

		MTLine b = new MTLine(mtApplication, 180 + len, 80, 180 + len
				- arrowsize, 80 + arrowsize / 2);
		b.setStrokeColor(MTColor.BLACK);
		innerRightRectangleA.addChild(b);

		MTLine c = new MTLine(mtApplication, 180 + len, 80, 180 + len
				- arrowsize, 80 - arrowsize / 2);
		c.setStrokeColor(MTColor.BLACK);
		innerRightRectangleA.addChild(c);

		textField.registerInputProcessor(new TapProcessor(mtApplication));
		textField.addGestureListener(TapProcessor.class,
				new IGestureEventListener() {
					public boolean processGestureEvent(MTGestureEvent ge) {
						TapEvent te = (TapEvent) ge;
						IMTComponent3D target = te.getTarget();
						if (target instanceof MTTextArea) {
							MTTextArea ta = (MTTextArea) target;
							switch (te.getTapID()) {
							case TapEvent.TAPPED:
								ta.setText("ETAT A clic !");
								break;
							default:
								break;
							}
						}
						return false;
					}
				});
		// Add the textfield to our canvas
		innerRightRectangleA.addChild(textField);

		// Center the textfield on the screen
		textField.setPositionRelativeToParent(new Vector3D(90, 10));

		// MTEllipse circle = new MTEllipse(mtApplication, new Vector3D(70, 90),
		// 100, 100);
		// this.getCanvas().addChild(circle);

		// MTRadialMenu radialMenu = new MTRadialMenu(mtApplication, new
		// Vector3D(300, 300), 20);
		// this.getCanvas().addChild(radialMenu);

//		Note note = new Note(15, new Point(400, 400), "Hello Note ! Bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ");
//		NoteViewImpl noteView = new NoteViewImpl(mtApplication, note);
//		this.getCanvas().addChild(noteView);
//		
//		Note note2 = new Note((long) 16, new Point(200, 400), new Dimension(300, 300), "Hello Note ! Bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ");
//		NoteViewImpl noteView2 = new NoteViewImpl(mtApplication, note2);
//		sheet.addChild(noteView2);
	}

	private void drawGrid(MTApplication mtApplication, int height, int width,
			int espacementGrille, MTColor color) {
		int currentWidth = espacementGrille;
		int currentHeight = espacementGrille;

		while (currentWidth < width) {
			currentHeight = espacementGrille;
			while (currentHeight < height) {
				MTEllipse circle = new MTEllipse(mtApplication, new Vector3D(
						currentWidth, currentHeight), 1, 1);
				circle.setFillColor(color);
				circle.removeAllGestureEventListeners();
				this.getCanvas().addChild(circle);
				currentHeight += espacementGrille;
			}
			currentWidth += espacementGrille;
		}
	}

	public void init() {
	}

	public void shutDown() {
	}
}
